package ch13;

import java.util.ArrayList;

// Deposit.java
// Represents a deposit ATM transaction

public class Deposit extends Option
{
	
	
   // irrelevant stringarray for menu-visualization
   private ArrayList<String> stringArray = new ArrayList<String>();
	
   private double amount; // amount to deposit
   private Keypad keypad; // reference to keypad
   private DepositSlot depositSlot; // reference to deposit slot
   private final static int CANCELED = 0; // constant for cancel option

   // Deposit constructor
   public Deposit( int userAccountNumber, Screen atmScreen, 
      BankDatabase atmBankDatabase, Keypad atmKeypad, 
      DepositSlot atmDepositSlot )
   {
      // initialize superclass variables
      super( userAccountNumber, atmScreen, atmBankDatabase );

      // initialize references to keypad and deposit slot
      keypad = atmKeypad;
      depositSlot = atmDepositSlot;
   } // end Deposit constructor

   // perform transaction
   @Override
   public void execute()
   {
    //  BankDatabase bankDatabase = getBankDatabase(); // get reference
      Screen screen = getScreen(); // get reference
      BankDatabase bankDatabase = getBankDatabase(); // get reference

      amount = promptForDepositAmount(); // get deposit amount from user

      // check whether user entered a deposit amount or canceled
      if ( amount != CANCELED )
      {
         // request deposit envelope containing specified amount
    	  stringArray.clear();
		  stringArray.add("Please insert a deposit envelope containing ");
		  stringArray.add("$     " + amount);
		  screen.setKasse(stringArray, 10);

         // receive deposit envelope
         boolean envelopeReceived = depositSlot.isEnvelopeReceived();

         // check whether deposit envelope was received
         if ( envelopeReceived )
         {  bankDatabase.credit(getUserID(), amount);
        	stringArray.clear();
		    stringArray.add("Your envelope has been received ");
		    stringArray.add("NOTE: The money just deposited will not ");
		    stringArray.add("be available until we verify the amount of any  ");
		    stringArray.add("enclosed cash and your checks clear.");
		    screen.setKasse(stringArray, 10);
        	 
            
      
         } // end if
         else // deposit envelope not received
         {
        	stringArray.clear();
 		    stringArray.add("You did not insert an ");
 		    stringArray.add("envelope, so the ATM has canceled your transaction. ");
 		    screen.setKasse(stringArray, 10);
         } // end else
      } // end if 
      else // user canceled instead of entering amount
      {
    	  stringArray.clear();
		    stringArray.add("Cannceling transaction... ");
		    screen.setKasse(stringArray, 10);
      } // end else
   } // end method execute

   // prompt user to enter a deposit amount in cents 
   private double promptForDepositAmount()
   {
      Screen screen = getScreen(); // get reference to screen

      // display the prompt
      stringArray.clear();
	  stringArray.add("Please enter a deposit amount in Cents  ");
	  stringArray.add("Press 0 to cancel ");
	  screen.setKasse(stringArray, 10);
    
      int input = keypad.getInput(); // receive input of deposit amount
      
      // check whether the user canceled or entered a valid amount
      if ( input == CANCELED ) 
         return CANCELED;
      else
      {
         return ( double ) input / 100; // return dollar amount 
      } // end else
   } // end method promptForDepositAmount
} // end class Deposit
